package me.rvj.servlet;

import me.rvj.entity.ServerSocket;
import me.rvj.entity.User;
import net.sf.json.JSONArray;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @program: JavaWeb
 * @description: 获取在线用户列表
 * @author: Rv_Jiang
 * @date: 2021/7/2 10:37
 */
@WebServlet(name = "GetOnlineUserServlet",value = "/GetOnlineUserServlet")
public class GetOnlineUserServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        List<String> list = new ArrayList<>();
        list.add("☺猫窝之家☺");
        for (Map.Entry<String, User> item: ServerSocket.onlineUserList.entrySet()) {
            if(item.getValue().getSession()!=null){
                list.add(item.getValue().getUserName());
            }
        }
        JSONArray userList_Json = JSONArray.fromObject(list);
        resp.getWriter().print(userList_Json);
    }

}
